﻿// -----------------------------------------------------------------------
// <copyright file="CompressorTest.cs">
// Copyright (c) 2013 Andrey Veselov. All rights reserved.
// License:  Microsoft Public License (MS-PL)
// Contacts: http://andrey.moveax.com  andrey@moveax.com
// </copyright>
// -----------------------------------------------------------------------

namespace PortableImagingLibrary.UnitTests
{
    using System;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Png;
    using Png.Compressors;

    [TestClass]
    public class CompressorTest
    {
        [TestMethod]
        public void CompressAndDecompress()
        {
            // Arrange
            var compressor = CompressorFactory.CreateCompressor(CompressionType.Deflate);
            var decompressor = CompressorFactory.CreateDecompressor(CompressionType.Deflate);

            var random = new Random(DateTime.Now.Millisecond);
            var dataLength = random.Next(1024, 1024 * 2);
            var sourceData = new byte[dataLength];
            for (var i = 0; i < dataLength; i++)
                sourceData[i] = (byte)random.Next(255);

            // Act
            compressor.AddLine(sourceData);
            var compressed = compressor.GetChunkData();

            decompressor.BytesPerLine = dataLength;
            decompressor.SetCurrentChunkData(compressed);
            decompressor.ReadLine();
            var result = decompressor.OutputBuffer;

            // Assert
            Assert.AreEqual(sourceData.Length, result.Length);
            Assert.IsTrue(sourceData.SequenceEqual(result));
        }
    }
}
